package com.chukun.disruptor.netty.handler;

import com.chukun.disruptor.netty.bean.TranslatorData;
import com.chukun.disruptor.netty.disruptor.factory.RingBufferWorkerPoolFactory;
import com.chukun.disruptor.netty.disruptor.producer.MessageProducer;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 定义数据传输的handler
 */
public class ServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        TranslatorData request = (TranslatorData) msg;
//        System.err.println("netty server receive data form client  id : "+request.getId()
//                +" name: "+request.getName()+" message: "+request.getMessage());
//
//        //回写到客户端通知
//        TranslatorData response = new TranslatorData();
//        response.setId("resp: " + request.getId());
//        response.setName("resp: " + request.getName());
//        response.setMessage("resp: " + request.getMessage());
//        System.err.println("write the response data: ");
//        ctx.writeAndFlush(response);
        TranslatorData translatorData = (TranslatorData) msg;
        //创建一个生产者的ID,必须唯一
        String producerID = "code:sessionID:001";
        MessageProducer messageProducer = RingBufferWorkerPoolFactory.getInstance().getMessageProducer(producerID);
        messageProducer.onData(translatorData,ctx);
    }
}
